var pageSize = 10;
//职称类别
var sexCodeMap = {};
var nationCodeMap = {};
var rowid=null;
$(document).ready(function(){
	commonSet();
	//表格初始化
	initTable();
	//事件绑定
	eventBind();
});

function commonSet(){
	initLoader();
	genDictSelect('sexInfo','sex','1');
	sexCodeMap =getTransParam('sexInfo');
	nationCodeMap =getTransParam('nationCode');
}

//事件绑定
function eventBind(){
	//带有分页信息的查询
	$("#queryBtn").click(pageQuery).click();
	
	//添加按钮弹出层
	$("#addBtn").click(showAddFormDialog);
	
	//挂号弹出层
	$("#regBtn").click(showRegFormDialog);
}

//表格初始化
function initTable(){
	$('#patientListTable').bootstrapTable({
		onPageChange: function (number, size) {
			pageSize = size;
			query(number, size);
		},
		onCheck:function (row) {
			rowid=row.id;
		}
	});
}

//带有分页信息的查询
function pageQuery(){
	query(1, pageSize);
};

//分页查询
function query(pageNumber, pageSize){
	var queryCondition={
		pageNumber: pageNumber, 
		pageSize: pageSize,
		params:$("#patientForm").collect()
	}
	
	$.getApiClient('/patient/list').setQuery(queryCondition).post().done(function(data){
	    $('#patientListTable').bootstrapTable('load', data);
	});
};

//添加医院弹出
function showAddFormDialog(){
	customRemoteUrlDialog('添加患者','/page/business/patient/form?type=add',null);
}

//修改医院弹出
function showEditFormDialog(row){
	customRemoteUrlDialog('患者详情','/page/business/patient/view?pId='+row.id,null);
}

function showRegFormDialog(){
	if(rowid==null){//增加提示
		customTimeOutDialog('信息提示','请选择患者后进行操作',null,2000);
	}else{
		redirectCall('/page/business/remote/remoteReg?pId='+rowid,'/page/business/remote/list');
//		customRemoteUrlDialog('远程预约','/page/business/remote/remoteReg?pId='+rowid,null);
	}
}

//删除医院弹出
function showDelFormDialog(row){
	customConfirmDialog('是否确认删除患者？','删除患者',null,function(dialog){
		//执行删除
		dialog.close();
		doDeleteHos(row.id,function(data){
			if(data.code == "0"){
				customTimeOutDialog('信息提示','删除患者成功',null,2000);
				pageQuery();
			}else{
				customAlertDialog(data.message, '错误提示', null, null);
			}
		})
	});
	
	function doDeleteHos(costId,callBack){
		$.getApiClient("/patient/delete/{id}").setQuery({
			id : costId
		}).del().done(function(data){
			callBack(data)
		});
	}
}

//操作列
function operateFormatter(value, row, index) {	
	var operates=[];
	operates.push('<a class="edit" href="javascript:void(0)" title="患者详细"><i class="fa fa-edit"></i>患者详细</a>');
	return operates.join('');
};


operateEvents = {
	'click .edit' : function(e, value, row, index) {
		showEditFormDialog(row);
	}
};